<?php

namespace app\api\controller;

use app\api\validate\yan\CommentValidate;
use app\common\controller\UserBase;
use app\common\helper\SendSmsHelper;
use app\common\helper\WxHelper;
use app\common\logic\common\CommonLogic;
use app\common\logic\common\PayLogic;
use app\common\logic\OrderLogic;
use app\common\logic\UserLogic;
use app\common\model\Address;
use app\common\model\Attachment;
use app\common\model\Group;
use app\common\model\GroupOrder;
use app\common\model\MoneyLog;
use app\common\model\VipOrder;
use app\common\model\Withdraw;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;

class User extends UserBase
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\common\model\User();
    }
    // 开通Vip
    public function buy()
    {
        $id = input("id/d");
        $vip = \app\common\model\Vip::find($id);
        if (!$vip) $this->error();
        try {
            $vipOrder = new VipOrder();
            $vipOrder->user_id = $this->uid;
            $vipOrder->vip_id = $vip->id;
            $vipOrder->money = $vip->money;
            $vipOrder->xianqi = $vip->xianqi;
            $vipOrder->order_no = CommonLogic::makeOrderNo();
            $vipOrder->type = 'wanx';
            $vipOrder->save();
            $pay_id = PayLogic::createOrUpdatePayOrder($vipOrder->id);
            if (!$pay_id) $this->error();
            $data['openid'] = $this->user->openid;
            $data['pay_id'] = $pay_id;
            $data['user_id'] = $this->uid;
            $result = OrderLogic::createPay($data);
            $this->success($result);
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    // 充值token
    public function buyQwen()
    {
        $id = input("id/d");
        $vip = \app\common\model\VipQwen::find($id);
        if (!$vip) $this->error();
        try {
            $vipOrder = new VipOrder();
            $vipOrder->user_id = $this->uid;
            $vipOrder->vip_id = $vip->id;
            $vipOrder->money = $vip->money;
            $vipOrder->xianqi = $vip->xianqi;
            $vipOrder->order_no = CommonLogic::makeOrderNo();
            $vipOrder->type = 'qwen';
            $vipOrder->save();
            $pay_id = PayLogic::createOrUpdatePayOrder($vipOrder->id);
            if (!$pay_id) $this->error();
            $data['openid'] = $this->user->openid;
            $data['pay_id'] = $pay_id;
            $data['user_id'] = $this->uid;
            $result = OrderLogic::createPay($data);
            $this->success($result);
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    public function updateUser()
    {
        UserLogic::updateUserInfo($this->data);
        $this->success();
    }

    public function getAddress()
    {
        $list = Address::where("user_id",$this->uid)->order('id desc')->select();
        $this->success($list);
    }

    public function addAddress()
    {
        try {
            $this->validate($this->data,\app\validate\Address::class);
            if (!empty($this->data['id'])) {
                $address = Address::update($this->data);
            } else {
                $address = Address::create($this->data);
            }
            $this->success($address);
        } catch (ValidateException |Exception $exception) {
            $this->error($exception->getMessage());
        }
    }


    // 获取手机号
    public function getPhoneNumber()
    {
        $data = input();
        $wx = new WxHelper();
        $phone = $wx->getPhoneNumber($data['code']);
        UserLogic::updateUserInfo(['tel'=>$phone,'user_id'=>$this->uid]);
        $user = $this->model->find($this->uid);
        $this->success($user);
    }

    // 获取用户信息
    public function getUserInfo()
    {
        $this->success(UserLogic::getUserInfo($this->uid));
    }

    // 首次评价给仙气
    public function setPing()
    {
        UserLogic::setPing($this->uid);
        $this->success();
    }
    // 签到创作分享给仙气
    public function setSign()
    {
        $type = input('type');
        UserLogic::setSign($this->uid,$type);
        $this->success();
    }
    // 获取今天的情况
    public function getSign()
    {
        $data = UserLogic::getSign($this->uid);
        $this->success($data);
    }

    // 扩容仓库
    public function addStore()
    {
        UserLogic::addStore($this->user);
        $this->success();
    }

    // 作品库
    public function getImageList()
    {
        $list = UserLogic::getImageList($this->data);
        $this->success($list);
    }

    public function changeStore()
    {
        UserLogic::changeStore($this->data,$this->user);
        $this->success();
    }

    public function delImage()
    {
        UserLogic::delImage($this->data);
        $this->success();
    }

    public function inviteLog()
    {
        $list = MoneyLog::where("user_id",$this->uid)
            ->where('is_invite',1)
            ->order('id desc');
        if (!empty($this->data['create_time']) && $this->data['create_time'] !== 'all') {
            if ($this->data['create_time'] == 'month') $list = $list->whereMonth('create_time');
            if ($this->data['create_time'] == 'week') $list = $list->whereWeek('create_time');
        }
        $list =  $list->paginate(['list_rows'=>$this->limit,"page"=>$this->page]);
        $this->success($list);
    }

    public function getWithdraw()
    {
        $list = Withdraw::where('user_id',$this->uid)
            ->order('id desc');
        if (!empty($this->data['create_time']) && $this->data['create_time'] !== 'all') {
            if ($this->data['create_time'] == 'month') $list = $list->whereMonth('create_time');
            if ($this->data['create_time'] == 'week') $list = $list->whereWeek('create_time');
        }
        $list =  $list->paginate(['list_rows'=>$this->limit,"page"=>$this->page]);
        $this->success($list);
    }

    public function withdraw()
    {
        $rule = [
            'money|金额' => 'require|>:0',
            'type|提现类型' => 'require',
            'name|姓名' => 'require',
            'account|账号' => 'require',
        ];
        Db::startTrans();
        try {
            $this->validate($this->data,$rule);
            if ($this->user->money < $this->data['money']) {
                $this->error('余额不足');
            }
            Withdraw::create([
                'user_id' => $this->uid,
                'money' => $this->data['money'],
                'account' => $this->data['account'],
                'name' => $this->data['name'],
                "type" => $this->data['type']
            ]);
            $this->user->money -= $this->data['money'];
            $this->user->save();
            MoneyLog::create([
                'user_id' => $this->uid,
                'is_add' => 0,
                'content' => "提现",
                'money' => $this->data['money'],
            ]);
            Db::commit();
            $this->success();
        } catch (ValidateException | Exception $exception) {
            Db::rollback();
            $this->error($exception->getMessage());
        }
    }
    
    public function bindShareUser()
    {
        $share_id = input("share_id/d");
        if ($share_id) {
            $user = \app\common\model\User::where('id',$share_id)->find();
            if ($user && $this->user->pid == 0) {
                $this->user->pid = $share_id;
                $this->user->save();
                $this->success();
            }
        }
        $this->success();
    }
}